package com.aaa.util;

import com.aaa.entity.Emp;
import com.aaa.entity.ExamPerson;
import com.aaa.entity.Massage;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



public class EmpUtil {
    /**获取HttpServletRequest
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    /**获取session
     * @return
     */
    public static HttpSession getSession(){
        HttpSession session=null;
        if (null==session) {
            session=getRequest().getSession();
        }
        return session;
    }


    //存储当前注册单位到 session中
    public static void SaveMessage(Massage message){
        getSession().setAttribute("message",message);
    }

    //获取session中 当前登陆注册单位
    public static Massage getMessage(){
        return (Massage)getSession().getAttribute("message");
    }

    /**获取session里面的员工信息
     * @return
     */
    public static Emp getEmp(){
        Emp employee=(Emp) getSession().getAttribute("user");
        return employee;
    }
    /**登录成功以后向session 存入员工信息
     * @param
     */
    public static void  setEmp(Emp emp){
        getSession().setAttribute("user", emp);
    }


    /**
     * 退出登录，销毁session
     */
    public static void removeEmp(){
        getSession().invalidate();
    }

    /** 获取session里考试人员信息
     * @return
     */
    public static ExamPerson getExam(){
        ExamPerson examPerson= (ExamPerson) getSession().getAttribute("examPerson");
        return examPerson;
    }

    /** 登录成功以后向session 存入考试人员信息
     * @param person
     */
    public static void  setExamPerson(ExamPerson person){
        System.out.println(person.getUsername()+"   sess");
        System.out.println(person.getExam_id()+"   eid");
        getSession().setAttribute("examPerson", person);
    }

    /**
     * @param person
     */
    public static void saveExamPerson(ExamPerson person){
        getSession().setAttribute("examPerson",person);
    }

    /**判断是否为超管
     * @return
     */
    /*public static boolean isAdmin(){
        Employee employee=getEmp();
        if (null!=employee) {
            if (employee.getAdmin()==1) {
                return true;
            }else {
                return false;
            }
        }
        return false;
    }*/
}
